package com.yuyang.wifi.utils;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.app.KeyguardManager;
import android.content.Context;
import android.content.res.Configuration;
import android.graphics.Point;
import android.os.Build;
import android.os.Environment;
import android.telephony.TelephonyManager;
import android.util.DisplayMetrics;
import android.view.Display;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;


import com.blankj.utilcode.util.Utils;

import java.io.IOException;
import java.lang.reflect.Field;
import java.lang.reflect.Method;

public class DeviceUtil {

    public static String getDeviceName() {
//		String devName = android.os.Build.MANUFACTURER +"-"+ android.os.Build.PRODUCT;
        String devName = Build.MODEL;
        devName = devName.trim();
        return devName;
    }

    public static boolean existSDCard() {
        return Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED);
    }

    //获取手机屏幕真实高度
    public static int getRealHeight(Activity activity) {
        Display display = activity.getWindowManager().getDefaultDisplay();
        Point size = new Point();
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
            display.getRealSize(size);
        } else {
            display.getSize(size);
        }
        return size.y;
    }


    public static String getIMEI(Context sContext) {
        TelephonyManager telephonyManager = (TelephonyManager) sContext.getSystemService(Context.TELEPHONY_SERVICE);
        @SuppressLint("MissingPermission") String IMEI = telephonyManager.getDeviceId(); //IMEI码
        return IMEI;
    }

//    public static String getUniqueID(Context sContext) {
//        TelephonyManager telephonyManager = (TelephonyManager) sContext.getSystemService( Context.TELEPHONY_SERVICE);
//        @SuppressLint("MissingPermission") String m_IMEI = telephonyManager.getDeviceId(); //IMEI码
//
//        //这个同一个厂商同样设备同样的rom下会重复
//        String m_DEVID = "35" + //we make this look like a valid IMEI
//                Build.BOARD.length() % 10 + Build.BRAND.length() % 10
//                + Build.CPU_ABI.length() % 10 + Build.DEVICE.length() % 10
//                + Build.DISPLAY.length() % 10 + Build.HOST.length() % 10
//                + Build.ID.length() % 10 + Build.MANUFACTURER.length() % 10
//                + Build.MODEL.length() % 10 + Build.PRODUCT.length() % 10
//                + Build.TAGS.length() % 10 + Build.TYPE.length() % 10
//                + Build.USER.length() % 10; // 13 digits
//
//        WifiManager wm = (WifiManager) sContext.getSystemService( Context.WIFI_SERVICE);
//        String m_WLANMAC = wm.getConnectionInfo().getMacAddress();
//        String uniqueID = HashtabUtil.md5(m_IMEI + m_DEVID + m_WLANMAC);
//        return uniqueID;
//    }


    public static boolean isScreenChange(Context ctx) {
        Configuration mConfiguration = ctx.getResources().getConfiguration(); // 获取设置的配置信息
        int ori = mConfiguration.orientation; // 获取屏幕方向
        if (ori == Configuration.ORIENTATION_LANDSCAPE) {
            // 横屏
            return true;
        } else if (ori == Configuration.ORIENTATION_PORTRAIT) {
            // 竖屏
            return false;
        }
        return false;
    }


    public static boolean screenLocked(Context ctx) {
        KeyguardManager mKeyguardManager = (KeyguardManager) ctx.getSystemService(Context.KEYGUARD_SERVICE);
        boolean status = mKeyguardManager.inKeyguardRestrictedInputMode();
        return status;
    }


    /**
     * 根据手机的分辨率从 dp 的单位 转成为 px(像素)
     *
     * @param context 上下文
     * @param dpValue dp值
     * @return 转换后的px值
     */
    public static int dp2px(Context context, float dpValue) {
        final float scale = context.getResources().getDisplayMetrics().density;
        return (int) (dpValue * scale + 0.5f);
    }

    /**
     * 根据手机的分辨率从 px(像素) 的单位 转成为 dp
     *
     * @param context 上下文
     * @param pxValue px值
     * @return 转换后的dp值
     */
    public static int px2dp(Context context, int pxValue) {
//        WindowManager wm = (WindowManager) Utils.getApp().getSystemService(Context.WINDOW_SERVICE);
//        DisplayMetrics systemDM = new DisplayMetrics();
//        wm.getDefaultDisplay().getMetrics(systemDM);
        try {
            final float scale = context.getResources().getDisplayMetrics().density;
            return (int) ((float) pxValue / scale + 0.5f);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return 300;
    }

    /**
     * 获取屏幕分辨宽，以px为单位
     *
     * @param context 上下文
     * @return 屏幕宽
     */
    public static int getWidth(Context context) {
//        try {
//            return context.getResources().getDisplayMetrics().widthPixels;
//        } catch (Exception e) {
//            e.printStackTrace();
//        }
//        return 310;
        WindowManager wm = (WindowManager) Utils.getApp().getSystemService(Context.WINDOW_SERVICE);
        if (wm == null) return -1;
        Point point = new Point();
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
            wm.getDefaultDisplay().getRealSize(point);
        } else {
            wm.getDefaultDisplay().getSize(point);
        }
        return point.x;
    }

    public static int getScreenWidth(Context context) {
        WindowManager wm = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
        return wm.getDefaultDisplay().getWidth();
    }

    /**
     * 获取屏幕分辨高，以px为单位
     *
     * @param context 上下文
     * @return 屏幕高
     */
    public static int getHeight(Context context) {
        return context.getResources().getDisplayMetrics().heightPixels;
    }

    public static float getHeightByDp(Context context) {
        DisplayMetrics displayMetrics = context.getResources().getDisplayMetrics();
        return displayMetrics.heightPixels / displayMetrics.density;
    }

    /**
     * 获取状态栏高度 （如果手机支持状态栏）
     *
     * @param context
     * @return
     */
    public static int getStatusBarHeight(Context context) {
        if (Build.VERSION.SDK_INT < Build.VERSION_CODES.KITKAT) {
            return 0;
        }
        int height = 0;
        int resourceId = context.getResources().getIdentifier("status_bar_height", "dimen", "android");
        if (resourceId > 0) {
            height = context.getResources().getDimensionPixelSize(resourceId);
        } else
            height = 50;
        return height;
    }

    //设置状态栏字体为黑色
    public static void setLightStatusBar(Activity activity) {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            if (isMIUI()) {
                setMiuiStatusBarDarkMode(activity, true);
            }
            View decorView = activity.getWindow().getDecorView();
            int option = decorView.getSystemUiVisibility();
            option |= View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR;
            decorView.setSystemUiVisibility(option);
        }
    }

    public static boolean isMIUI() {
        String KEY_MIUI_VERSION_CODE = "ro.miui.ui.version.code";
        String KEY_MIUI_VERSION_NAME = "ro.miui.ui.version.name";
        String KEY_MIUI_INTERNAL_STORAGE = "ro.miui.internal.storage";
        try {
            final BuildProperties prop = BuildProperties.newInstance();
            return prop.getProperty(KEY_MIUI_VERSION_CODE, null) != null || prop.getProperty(KEY_MIUI_VERSION_NAME, null) != null
                    || prop.getProperty(KEY_MIUI_INTERNAL_STORAGE, null) != null;
        } catch (final IOException e) {
            return false;
        }
    }

    public static boolean setMiuiStatusBarDarkMode(Activity activity, boolean darkmode) {
        Class<? extends Window> clazz = activity.getWindow().getClass();
        try {
            int darkModeFlag = 0;
            Class<?> layoutParams = Class.forName("android.view.MiuiWindowManager$LayoutParams");
            Field field = layoutParams.getField("EXTRA_FLAG_STATUS_BAR_DARK_MODE");
            darkModeFlag = field.getInt(layoutParams);
            Method extraFlagField = clazz.getMethod("setExtraFlags", int.class, int.class);
            extraFlagField.invoke(activity.getWindow(), darkmode ? darkModeFlag : 0, darkModeFlag);
            return true;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return false;
    }
}
